<%@page import="cn.sendto.service.ResidentInfoService"%>
<%@page import="cn.sendto.model.ResidentInfoModel"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" errorPage="../err.jsp"%>
<jsp:include page="../lock.jsp"></jsp:include>	
<%
	request.setCharacterEncoding("UTF-8");
	String username = request.getParameter("username");
	String userpass = request.getParameter("userpass");
	String comuserpass = request.getParameter("comuserpass");
	String name = request.getParameter("name");
	String gender = request.getParameter("gender");
	String age = request.getParameter("age");
	String tellphone = request.getParameter("tellphone");
	String idcard = request.getParameter("idcard");
	//验证数据的有效性
	ArrayList<Integer> errlist = new ArrayList<Integer>();
	HashMap<String, String> formlist = new HashMap<String, String>();
	if (username == null || username.equals("")) {
		errlist.add(1);
	} else {
		formlist.put("username", username);
	}
	if (userpass == null || userpass.equals("")) {
		errlist.add(2);
	}
	if (comuserpass == null || comuserpass.equals("")) {
		errlist.add(3);
	}
	if (userpass != null && comuserpass != null && !userpass.equals("") && !comuserpass.equals("")
			&& !userpass.equals(comuserpass)) {
		errlist.add(4);
	}
	if (name == null || name.equals("")) {
		errlist.add(5);
	} else {
		formlist.put("name", name);
	}
	if (age == null || age.equals("")) {
		errlist.add(6);
	} else {
		formlist.put("age", age);
	}
	if (tellphone == null || tellphone.equals("")) {
		errlist.add(7);
	} else {
		formlist.put("tellphone", tellphone);
	}
	if (idcard == null || idcard.equals("")) {
		errlist.add(8);
	} else {
		formlist.put("idcard", idcard);
	}

	if (errlist.size() > 0) {
		request.setAttribute("info", errlist);
		request.setAttribute("form", formlist);
		request.getRequestDispatcher("addresident.jsp").forward(request, response);
		return;
	}

	ResidentInfoModel rim = new ResidentInfoModel();
	rim.setName(name);
	rim.setAge(Integer.parseInt(age));
	rim.setGender(gender);
	rim.setIdCard(idcard);
	rim.setTellphone(tellphone);
	rim.setUserName(username);
	rim.setUserPass(userpass);

	ResidentInfoService ris = new ResidentInfoService();
	int rows = ris.add(rim);
	if (rows > 0) {
		//返回注册页面
		request.setAttribute("addresidentinfo","<span style='color:red;'>"+"添加成功"+"</span>"+"，可以继续添加用户或者"+"<a href='residentlist.jsp'>点击此处返回业主列表</a>");
		request.getRequestDispatcher("addresident.jsp").forward(request, response);
		return;
	} else if (rows == -1) {
		request.setAttribute("addresidentinfo", "添加失败,用户名已存在");
		request.setAttribute("form", formlist);
		request.getRequestDispatcher("addresident.jsp").forward(request, response);
		return;
	} else {
		request.setAttribute("addresidentinfo", "添加失败。");
		request.getRequestDispatcher("addresident.jsp").forward(request, response);
		return;
	}
%>